package mypackage;

import java.util.Vector;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.FullScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

import com.dvg.scores.IHttpResponseListener;
import com.dvg.scores.Record;
import com.dvg.scores.ScoresModule;

public class MainScreen extends FullScreen implements FieldChangeListener, IHttpResponseListener {
	
	//private Record[] records;
	private Vector _records;
	private VerticalFieldManager vfm;
	private Manager lMgr;
	private ButtonField btnStart;
	
	private String jsonString;
	
	public MainScreen(){
		super(FullScreen.DEFAULT_MENU | FullScreen.DEFAULT_CLOSE);
		initUI();
		initSublayout();
		ScoresModule.getInstance().setResponseListener(this);
	}

	private void initUI() {
		this.jsonString = "[{\"userName\":\"Vika\",\"scores\":2147483647,\"levels\":100,\"stars\":3},{\"userName\":\"Petya\",\"scores\":2147483647,\"levels\":100,\"stars\":3},{\"userName\":\"Victor\",\"scores\":1600,\"levels\":12,\"stars\":2},{\"userName\":\"Vovan\",\"scores\":1234,\"levels\":12,\"stars\":0}]";
		this.btnStart = new ButtonField("Start", ButtonField.CONSUME_CLICK);
		this.btnStart.setChangeListener(this);
		this.vfm = new VerticalFieldManager(Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT | VerticalFieldManager.VERTICAL_SCROLL);
		
	}

	private void initSublayout() {
		lMgr = new Manager(Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT) {
			
			protected void sublayout(int width, int height) {
				layoutChild(btnStart, width, height);
				setPositionChild(btnStart, 0, 0);
				
				layoutChild(vfm, width, height);
				setPositionChild(vfm, 0, btnStart.getContentTop() + btnStart.getContentHeight() + 5);
				
				setExtent(480, 360);
			}
		};
		
		lMgr.add(btnStart);
		lMgr.add(vfm);
		
		add(lMgr);
	}

	public void fieldChanged(Field field, int context) {
		if (field == this.btnStart){
			//ScoresModule.getInstance().getSores(Consts.GANE_NAME);
			ScoresModule.getInstance().postScores(Consts.GANE_NAME, 99, 3, 10);
		}
	}

//	private void parceJSON(String source) {
//		try {
//			JSONArray records = new JSONArray(source);
//			this.records = new Record[records.length()];
//			for(int i = 0; i < records.length(); i++){
//				JSONObject obj = records.getJSONObject(i);
//				Record rec = new Record(obj.getString("userName"), obj.getInt("scores"), obj.getInt("levels"), obj.getInt("stars"));
//				LabelField lf = new LabelField(rec.toString(), Field.FOCUSABLE);
//				this.vfm.add(lf);
//			}
//		} catch (JSONException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}

	public void processResponse(final Record[] records) {
		if (records != null){
			UiApplication.getUiApplication().invokeAndWait(new Runnable() {
				
				public void run() {
					Dialog.inform(records.length + " records reseived!");
				}
			});
		}
	}
}
